<template>
    <div class="payFess-container">
      <el-card shadow="hover" :body-style="{ paddingBottom: '0' }"> 
        <el-form :model="queryParams" ref="queryForm" labelWidth="90">
          <el-row>
            <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
              <el-form-item label="关键字">
                <el-input v-model="queryParams.searchKey" clearable="" placeholder="请输入模糊查询关键字"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
              <el-form-item label="账单编号">
                <el-input v-model="queryParams.billNumber" clearable="" placeholder="请输入账单编号"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
              <el-form-item label="账单类目">
                 <el-select v-model="queryParams.itemBil" placeholder="请选择账单类目" @change="handleQuery">
                  <el-option  value="0">全部</el-option>
                  <el-option v-for="item in enumItemBills"  :value="item.key" :label="item.value"/>
                 </el-select>
              </el-form-item>
            </el-col>
            
            <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
              <el-form-item label="所属房屋">
                <el-select v-model="queryParams.houseId" placeholder="请选择所属房屋" @change="handleQuery">
                  <el-option  value="0">全部</el-option>
                  <el-option v-for="item in house"  :value="item.id" :label="item.houseName"/>
                 </el-select>      
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
              <el-form-item label="账单状态">
                <el-select v-model="queryParams.billingStatus" placeholder="请选择账单方式" @change="handleQuery">
                  <el-option  value="0">全部</el-option>
                  <el-option v-for="item in enumBilStatuss"  :value="item.key" :label="item.value"/>
                 </el-select>      
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
              <el-form-item >
                <el-button-group style="display: flex; align-items: center;">
                  
                  
                  <el-button type="primary"  icon="ele-Search" @click="handleQuery" >查询</el-button>&nbsp;
                        <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
                          <el-button icon="ele-ZoomIn" @click="changeAdvanceQueryUI" v-if="!showAdvanceQueryUI" style="margin-left:5px;"> 高级查询 </el-button>
                          <el-button icon="ele-ZoomOut" @click="changeAdvanceQueryUI" v-if="showAdvanceQueryUI" style="margin-left:5px;"> 隐藏 </el-button>
                  <!-- <el-button type="primary" style="margin-left:5px;" icon="ele-Plus" @click="openAddPayFess" v-auth="'payFess:add'"> 新增 </el-button>&nbsp; -->&nbsp;
                  <!-- <el-button type="primary" icon="ele-Promotion"  v-auth="'payFess:export'" @click="handleExport">导出</el-button> -->
                  <el-button type="primary" icon="ele-Promotion"   @click="handleExport">导出</el-button>
                </el-button-group>
              </el-form-item>
              
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <el-card class="full-table" shadow="hover" style="margin-top: 5px">
        <el-table
                  :data="tableData"
                  style="width: 100%"
                  v-loading="loading"
                  tooltip-effect="light"
                row-key="id"
                  @sort-change="sortChange"
                  border="">
              
          <el-table-column prop="id" label="流水号"  show-overflow-tooltip="" />
          <el-table-column prop="billNumber" label="账单编号"  show-overflow-tooltip="">
            <template v-slot="scope">
              {{ scope.row.billNumber ? scope.row.billNumber.replace(/(.{4})(\d{6})(.{4})/, '$1****$3') : '' }}
            </template>
          </el-table-column>
          <el-table-column prop="houseName" label="所属房屋"  show-overflow-tooltip="" />
          <el-table-column prop="itemBil" label="账单类目"  show-overflow-tooltip="">
            <template v-slot="scope">
              {{ scope.row.itemBil==1?"代缴电费":scope.row.itemBil==2?"代缴水费":scope.row.itemBil==3?"物业费":scope.row.itemBil==4?"卫生费":"车位费" }}
            </template>
          </el-table-column>
          <el-table-column prop="chargingcyclestart" label="计费周期"  show-overflow-tooltip="">
            <template v-slot="scope">
                {{ scope.row.chargingcyclestart.substr(0,10).replaceAll('-','') }}-{{ scope.row.chargingcycleEnd.substr(0,10).replaceAll('-','') }}
            </template>
          </el-table-column>
          <el-table-column prop="receivableTime" label="应收时间"  show-overflow-tooltip="">
            <template v-slot="scope">
                {{ scope.row.receivableTime.substr(0,10).replaceAll('-','') }}
            </template>
          </el-table-column>
          <el-table-column prop="amountReceivable" label="应收金额"  show-overflow-tooltip="">
            <template v-slot="scope">
              ￥{{ scope.row.amountReceivable.toFixed(2) }}
            </template>
          </el-table-column>
          <el-table-column prop="billingStatus" label="账单状态"  show-overflow-tooltip="">
            <template v-slot="scope">
                <span :style="{ backgroundColor: getStatusColor(scope.row.billingStatus), display: 'inline-block', width: '10px', height: '10px', marginRight: '5px' }"></span>
                {{ scope.row.billingStatus === 1 ? '未支付' : '已支付' }}
            </template>
          </el-table-column>
          <el-table-column  label="操作"  show-overflow-tooltip="">
            <template v-slot="scope">
              <el-button type="primary" icon="ele-Edit" @click="dataToEdit(scope.row.id)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
                  v-model:currentPage="tableParams.page"
                  v-model:page-size="tableParams.pageSize"
                  :total="tableParams.total"
                  :page-sizes="[10, 20, 50, 100, 200, 500]"
                  size="small"
                  background=""
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  layout="total, sizes, prev, pager, next, jumper"
      />
        <printDialog
          ref="printDialogRef"
          :title="printPayFessTitle"
          @reloadTable="handleQuery" />
        <editDialog
          ref="editDialogRef"
          :title="editPayFessTitle"
          @reloadTable="handleQuery"
        />
      </el-card>
    </div>
  </template>
  
  <script lang="ts" setup="" name="payFess">
    import { ref } from "vue";
    import { ElMessageBox, ElMessage } from "element-plus";
    import { auth } from '/@/utils/authFunction';
    import { getAPI } from '/@/utils/axios-utils';
  
    import { formatDate } from '/@/utils/formatTime';
  
  import { enumPayItemBills } from '/@/api/payfess/payFess';
  import { enumPaymentmethod } from '/@/api/payfess/payFess';
  import{excelExports} from '/@/api/payfess/payFess';
  import { enumPayBilStatus } from '/@/api/payfess/payFess';
  import { pageOrderPayFess } from "/@/api/payfess/payFess";    
  import { houseList } from "/@/api/payfess/payFess";
  import{orderExcelExports} from '/@/api/payfess/payFess';
  
    import printDialog from '/@/views/system/print/component/hiprint/preview.vue'
    import editDialog from '/@/views/payfess/payFess/component/editDialog.vue'
    import { pagePayFess, deletePayFess } from '/@/api/payfess/payFess';
  import axios from "axios";
import {useRouter} from "vue-router";

const router = useRouter();
  
    const showAdvanceQueryUI = ref(false);
    const printDialogRef = ref();
    const editDialogRef = ref();
    const loading = ref(false);
    const tableData = ref<any>([]);
    const queryParams = ref<any>({});
    const tableParams = ref({
      page: 1,
      pageSize: 10,
      field: 'createTime', // 默认的排序字段
      order: 'descending', // 排序方向
      descstr: 'descending', // 降序排序的关键字符
      total: 0 as any,
    });
  
    const printPayFessTitle = ref("");
    const editPayFessTitle = ref("");
  
    // 改变高级查询的控件显示状态
    const changeAdvanceQueryUI = () => {
      showAdvanceQueryUI.value = !showAdvanceQueryUI.value;
    }
  
    // 查询操作
    const handleQuery = async () => {
      loading.value = true;
      var res = await pageOrderPayFess(Object.assign(queryParams.value, tableParams.value));
      tableData.value = res.data.result?.items ?? [];
      tableParams.value.total = res.data.result?.total;
      loading.value = false;
    };
  
    // 列排序
    const sortChange = async (column: any) => {
      tableParams.value.field = column.prop;
      tableParams.value.order = column.order;
      await handleQuery();
    };
  
    // 打开新增页面
    // const openAddPayFess = () => {
    //   editPayFessTitle.value = '添加缴费';
    //   editDialogRef.value.openDialog({});
    // };
  
  
    // 删除
    // const delPayFess = (row: any) => {
    //   ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    //   confirmButtonText: "确定",
    //   cancelButtonText: "取消",
    //   type: "warning",
    // })
    // .then(async () => {
    //   await deletePayFess(row);
    //   handleQuery();
    //   ElMessage.success("删除成功");
    // })
    // .catch(() => {});
    // };
  
    // 改变页面容量
    const handleSizeChange = (val: number) => {
      tableParams.value.pageSize = val;
      handleQuery();
    };
  
    // 改变页码序号
    const handleCurrentChange = (val: number) => {
      tableParams.value.page = val;
      handleQuery();
    };
    //枚举账单类目下拉框
    const enumItemBills = ref<any>([]);
    const handEnums = async () => {
      var res = await enumPayItemBills(enumItemBills.value)
      enumItemBills.value = res.data.result;
  
    }
 
     //枚举装填下拉框
     const enumBilStatuss = ref<any>([]);
    const handEnumsThree = async () => {
      var res = await enumPayBilStatus(enumBilStatuss.value)
      enumBilStatuss.value = res.data.result;
    }
    //导出
    const handleExport = async () => {
  
      const res = await orderExcelExports(); 
  
      const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      const link = document.createElement('a');
      link.setAttribute('href', URL.createObjectURL(blob));
      link.setAttribute('download', '缴费订单列表.xlsx');
      link.style.visibility = 'hidden';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      URL.revokeObjectURL(link.href);
    }
    //账单状态背景颜色
    const getStatusColor = (status:any) =>{
        return status === 1 ? 'red' : 'green'; 
    }
    //获取房屋列表
    const house = ref<any>([]);
    const getHouseList = async () => {
        var res = await houseList()
        house.value = res.data.result;
    }
    //跳转页面  
    const dataToEdit = (id: any) => {
        router.push({ name: 'details', query: { pid: id } });
    }
  
  
    handleQuery();
    handEnums();
    handEnumsThree();
    getHouseList();
    
  </script>
  <style scoped>
  :deep(.el-input),
  :deep(.el-select),
  :deep(.el-input-number) {
      width: 100%;
  }
  </style>
  
  